﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ILabourLanguage
    {
        public DataSet LabourLanguage_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LabourLanguage_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LabourLanguage_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LabourLanguage_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public LabourLanguageInfo LabourLanguage_GetInfo(int LLID)
        {
            LabourLanguageInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LabourLanguage_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LL_ID", LLID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new LabourLanguageInfo();
                    retVal.ID = Convert.ToInt32(dr["LL_ID"]);                
                    retVal.EmployeeID = Convert.ToInt32(dr["LL_EmployeeID"]);
                    retVal.LanguageID = Convert.ToInt32(dr["LL_LanguageID"]);
                    retVal.LanguageLevel = dr["LL_LanguageLevel"].ToString();
                    retVal.Description = dr["LL_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int LabourLanguage_Insert(LabourLanguageInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LabourLanguage_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LL_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@LL_LanguageID", eInfo.LanguageID);
            dbCmd.Parameters.AddWithValue("@LL_LanguageLevel", eInfo.LanguageLevel);
            dbCmd.Parameters.AddWithValue("@LL_Description", eInfo.Description);
            
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool LabourLanguage_Update(LabourLanguageInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LabourLanguage_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LL_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@LL_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@LL_LanguageID", eInfo.LanguageID);
            dbCmd.Parameters.AddWithValue("@LL_LanguageLevel", eInfo.LanguageLevel);
            dbCmd.Parameters.AddWithValue("@LL_Description", eInfo.Description);
            
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool LabourLanguage_Delete(int LLID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LabourLanguage_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LL_ID", LLID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
